package com.ruike.alisurface.ui.settings;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.widget.ImageView;

import com.ruike.alisurface.R;
import com.ruike.alisurface.base.BaseAty;
import com.ruike.alisurface.utils.GsonUtils;
import com.ruike.alisurface.utils.QrcodeUtils;
import com.voodoo.lib_utils.DeviceUtils;
import com.voodoo.lib_utils.imageLoader.GlideImageLoader;

import java.util.HashMap;

import butterknife.BindView;

/**
 * 绑定机器页
 */
public class BindMachine_Aty extends BaseAty {

    @BindView(R.id.bindMachine_bindUrlQrCode_imgv)
    ImageView bindMachineBindUrlQrCodeImgv;
    @BindView(R.id.bindMachine_snQrCode_imgv)
    ImageView bindMachineSnQrCodeImgv;

    @Override
    protected int getLayoutResId() {
        return R.layout.app_bind_machine;
    }

    @Override
    protected void initViews() {

     }

    @Override
    protected void initData() {
        initTitleBar(true);
//        GlideImageLoader.getInstance().loadBitmap(this, createQrCode("http://m.shouhuojiyun.com"), bindMachineBindUrlQrCodeImgv);

        HashMap<String, String> map = new HashMap<>();
        map.put("version", "Android");
        map.put("sn", DeviceUtils.getDeviceSn());
        String jsonSN = GsonUtils.toJson(map, HashMap.class);
        GlideImageLoader.getInstance().loadBitmap(this, createQrCode(jsonSN), bindMachineSnQrCodeImgv);
        bindMachineSnQrCodeImgv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Map<String, Object> coflist = new HashMap<String, Object>();
//                coflist.put("sn", DeviceUtils.getDeviceSn());
//                String data = GsonUtils.StringMapToJson(coflist);
//                MqttOperation.publishMessage(Constant.Mqtt.BINDSUSCCESS, data);
            }
        });

    }


    /**
     * 创建二维码
     *
     * @param contentStr 二维码内容
     * @return Bitmap
     */
    private Bitmap createQrCode(String contentStr) {
        return QrcodeUtils.createQrCode(contentStr, 200, 200, BitmapFactory.decodeResource(getResources(), 0));
    }

}
